<template>
  <div style="margin: 20px auto;width: 98%">
    <div style="width: 40%;margin: 20px auto">
      邮箱：<el-input v-model="queryParams.email" placeholder="支持模糊查询  "></el-input>
      <el-button type="primary" icon="el-icon-search" circle style="margin-left: 5px" @click="memberSearch"></el-button>
    </div>
    <div style="margin: 20px auto;width: 98%">
      <el-button type="primary" @click="openFahuo">发 货</el-button>
    </div>
    <div style="margin: 20px auto;width: 98%">
      <el-table
        :data="tableData"
        v-loading="loading"
        stripe
        style="width: 100%">
        <el-table-column prop="userName" label="用户名" style="width: 20%" align="center"/>
        <el-table-column prop="email" label="邮箱" style="width: 20%" align="center"/>
        <el-table-column label="状态" style="width: 10%" align="center">
          <template v-slot="scope">
            <span v-if="scope.row.status === '0'">正 常</span>
            <span v-else>不 正 常</span>
          </template>
        </el-table-column>
        <el-table-column prop="wallet" label="余额" style="width: 20%" align="center"/>
        <el-table-column label="消息通知" style="width: 20%" align="center">
          <template v-slot="scope">
            <span v-if="scope.row.notepadCount === 0">0</span>
            <el-button v-else type="text" style="color: red" @click="openDialogCard(scope.row)">{{ scope.row.notepadCount }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" style="width: 10%" align="center">
          <template v-slot="scope">
            <el-button type="danger" size="small" @click="resetPass(scope.row)">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getMember"
      />

      <!--消息通知的弹窗-->
      <el-dialog
        title="通知内容"
        :visible.sync="dialogCard.visible"
        width="50%">
        <el-table
          :data="dialogCard.table"
          style="width: 100%">
          <el-table-column prop="sendTime" label="申请时间" align="center" />
          <el-table-column prop="message" label="申请内容" align="center" />
          <el-table-column label="操作" align="center">
            <template v-slot="scope">
              <el-button @click="refuseHandler(scope.row)">拒 绝</el-button>
              <el-button type="primary" @click="acceptHandler(scope.row)">同 意</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>

      <!--发货的弹窗-->
      <el-dialog
        title="发货窗口"
        :visible.sync="fahuo.visible"
        width="50%">
        <el-table
          :data="fahuo.table"
          style="width: 100%">
          <el-table-column prop="dealTime" label="购买时间" align="center" />
          <el-table-column prop="orderNumber" label="订单号" align="center" />
          <el-table-column prop="cost" label="总金额" align="center" />
          <el-table-column label="操作" align="center">
            <template v-slot="scope">
              <el-button type="primary" @click="submitHuo(scope.row)">发 货</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import { getMembers, getNotepads, handleCharge,resetPassword,getMemberSearch,getFahuo,sendHuo } from "@/api/xiaomi-mall/member"

export default {
  name: "index",
  data() {
    return {
      tableData: [
        // {
        //   id: "2",
        //   userName: "阿桂",
        //   email: "1083072565@qq.com",
        //   status: "正常",
        //   wallet: 5000,
        //   notepadCount: 5
        // }
      ],
      dialogCard: {
        visible: false,
        table: [
          // {
          //   sendTime: "2021-11-15 13:20:15",
          //   content: "充值500"
          // }
        ]
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        email: undefined,
        status: undefined
      },
      // 查询结果总条数
      total: 0,
      // 遮罩层
      loading: true,
      fahuo: {
        visible: false,
        table: []
      }
    }
  },
  created() {
    this.getMember()
  },
  methods: {
    /** 读取会员列表 */
    getMember() {
      getMembers(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    /** 获取待处理的消息通知 */
    getNotepad(senderId) {
      getNotepads(senderId).then((response) => {
          this.dialogCard.table = response.notepadList
      })
    },
    /** 打开消息通知的对话框 */
    openDialogCard(row) {
      this.dialogCard.table = []
      let senderId = row.userId
      this.getNotepad(senderId)
      this.dialogCard.visible = true;
    },
    // 拒绝申请
    refuseHandler(row) {
      handleCharge(row.id,2).then((response) => {
        alert(response.message)
        this.dialogCard.visible = false;
        this.dialogCard.table = []
        this.getMember()
      })
    },
    // 接受申请
    acceptHandler(row) {
      handleCharge(row.id, 1).then((response) => {
        alert(response.message)
        this.dialogCard.visible = false;
        this.dialogCard.table = []
        this.getMember()
      })
    },
    // 重置密码
    resetPass(row) {
      this.$confirm('你确认要重置'+ row.email + '的密码吗？','重置密码',{
        distinguishCancelAndClose: true,
        confirmButtonText: '重置',
        cancelButtonText: '放弃'
      }).then(() => {
        resetPassword(row.userId).then((response) => {
          alert(response.message)
          this.getMember()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已撤销重置密码'
        })
      })
    },
    // 搜索框方法
    memberSearch() {
      getMemberSearch(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    // 打开发货窗口
    openFahuo() {
      getFahuo().then((response) => {
        this.fahuo.table = response.orderList;
      })
      this.fahuo.visible = true;
    },
    submitHuo(row) {
      sendHuo(row.orderNumber).then((response) => {
        alert(response.message)
        this.openFahuo()
      })
    }

  }
}
</script>

<style scoped>

.el-input {
  width: 50%;
}

</style>
